﻿using System.Web.Mvc;

namespace WXY.Course.Common.MVCExtension
{
    internal class WebUrlPager : UrlPager
    {
        private string hrefPattern = "<a href=\"{0}\" class=\"{1}\">{2}</a>";
        private string nostylePattern = "<a href=\"{0}\">{1}</a>";
        public WebUrlPager(HtmlHelper htmlHelper, PagerConfig pageConfig, object urlParams)
            : base(htmlHelper, pageConfig , urlParams) { }

        public override string Render()
        {
            if (!config.NeedPage) return "";

            //首页&上一页
            if (config.CurrentPage > 1)
            {
                htmlAppender.AppendFormat(hrefPattern, GetUrl(config.FirstPage), "page_fh", "首页");
                htmlAppender.AppendFormat(hrefPattern, GetUrl(config.PrevPage), "page_fh", "上一页");
            }


            //页码
            foreach (var item in pageNumbers)
            {
                if (item == config.CurrentPage)
                    htmlAppender.AppendFormat("<a class=\"page_on\">{0}</a>", item);
                else
                    htmlAppender.AppendFormat(nostylePattern, GetUrl(item), item);
            }

            //下一页&末页
            if (config.CurrentPage < config.LastPage)
            {
                htmlAppender.AppendFormat(hrefPattern, GetUrl(config.NextPage), "page_fh", "下一页");
                htmlAppender.AppendFormat(hrefPattern, GetUrl(config.LastPage), "page_fh", "尾页");
            }

            htmlAppender.AppendFormat("<span>共 {0} 页</span>", config.PageCount);

            //htmlAppender.Append("<label class=\"gy_page_txt\">跳转到：</label>");
            //htmlAppender.AppendFormat("<input id=\"page_input\" type=\"text\" class=\"gy_page_in\" url-pattern=\"{0}\">", GetUrl("InputPageNumber"));
            //htmlAppender.Append("<input id=\"page_redirect\" type=\"button\" class=\"gy_page_btn\" value=\"GO\">");
            //htmlAppender.Append("<script type=\"text/javascript\">require(['jquery'],function($){$('#page_redirect').click(");
            //htmlAppender.Append("function(){location.href=$('#page_input').attr('url-pattern').replace('InputPageNumber', $('#page_input').val());})})</script>");

            return htmlAppender.ToString();
        }
    }
}
